package logic.facade;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import logic.service.StudentService;
import vo.IValueObject;
import vo.StudentVO;

/**
 *
 * @author Alexander
 */
public class StudentFacade extends Facade {

    public StudentFacade() {
        setService(new StudentService());
    }

    @Override
    protected boolean exist(IValueObject vo) {
        return !(super.find(((StudentVO) vo).getUsername()) == null);
    }
    
    public StudentVO login(StudentVO vo) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            return ((StudentService)getService()).login(vo, em);
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
        }
    }
    
    /**
     * Busca un usuario basado en la IP
     * @param vo Ip del computador el usuario
     * @return El usuario buscado
     */
    public StudentVO findByIp(String vo){
        EntityManagerFactory emf = null;
        EntityManager em = null;
        StudentVO al = null;
        try{
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            al = ((StudentService)getService()).findByIp(vo, em);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(em != null){
                em.clear();
                em.close();
            }
            return al;
        }
    }
}
